# -*- coding: utf-8 -*-
from django.db.models import Q
from rest_framework import serializers

from app.emergency.models import AreaPlanTypes
from dvadmin.utils.json_response import SuccessResponse, ErrorResponse
from dvadmin.utils.pagination import CustomPagination
from dvadmin.utils.serializers import CustomModelSerializer
from dvadmin.utils.viewset import CustomModelViewSet


class AreaPlanTypesSerializer(CustomModelSerializer):
    """
    区域预案细分预案-序列化器
    """
    plan_type_name = serializers.CharField(source='plantype.name', read_only=True)
    road_name = serializers.CharField(source='areaplan.struct.roadid.cname', read_only=True)
    road_id = serializers.CharField(source='areaplan.struct.roadid.id', read_only=True)
    struct_name = serializers.CharField(source='areaplan.struct.sname', read_only=True)
    plan_name = serializers.CharField(source='areaplan.name', read_only=True)
    device_count = serializers.SerializerMethodField()

    def get_device_count(self, obj):
        count = obj.deviceplancount
        return count

    class Meta:
        model = AreaPlanTypes
        fields = "__all__"
        read_only_fields = ["id"]


class AreaPlanTypesCreateUpdateSerializer(CustomModelSerializer):
    """
    区域预案细分预案  创建/更新时的列化器
    """

    class Meta:
        model = AreaPlanTypes
        fields = '__all__'


class AreaPlanTypesViewSet(CustomModelViewSet):
    """
    区域预案细分预案
    list:查询
    create:新增
    update:修改
    retrieve:单例
    destroy:删除
    """
    queryset = AreaPlanTypes.objects.order_by('-create_datetime')
    serializer_class = AreaPlanTypesSerializer

    # pagination_class = CustomPagination

    def get_plan_type_by_condition(self, request, *args, **kwargs):
        '''
            根据传入参数，获取区域预案细分预案信息
            road_id     :路段id
            struct_id   :位置id
            type_id     :预案类型
            status      :状态
        '''
        params = request.query_params
        road_id = params.get('road_id', None)
        struct_id = params.get('struct_id', None)
        type_id = params.get('type_id', None)
        status = params.get('status', None)
        instance = AreaPlanTypes.objects.all()
        if road_id:
            instance = instance.filter(areaplan__struct__roadid_id=road_id)
        if struct_id:
            instance = instance.filter(areaplan__struct__id=struct_id)
        if type_id:
            instance = instance.filter(plantype__id=type_id)
        if status:
            if status.lower() == 'true':
                instance = [item for item in instance if item.deviceplancount > 0]
            else:
                instance = [item for item in instance if item.deviceplancount == 0]
        page = self.paginate_queryset(instance)
        if page is not None:
            serializer = AreaPlanTypesSerializer(page, many=True, request=request)
            return self.get_paginated_response(serializer.data)
        serializer = AreaPlanTypesSerializer(instance, many=True, request=request)
        return SuccessResponse(data=serializer.data, msg="获取成功")

